using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;


public class CameraRenderer 
{
    /// <summary>渲染上下文 用于管理</summary>
    private ScriptableRenderContext _context;
    private Camera _camera;

    private CommandBuffer _cmd=new CommandBuffer();
    private CullingResults _cullingResults;
    
    static ShaderTagId _unlitShaderTagId = new ShaderTagId("SRPDefaultUnlit");

    public void Render(ScriptableRenderContext context, Camera camera)
    {
        this._context = context;
        this._camera = camera;
        
        context.SetupCameraProperties(_camera);
        
        _cmd.ClearRenderTarget(true, true, Color.black);
        context.ExecuteCommandBuffer(_cmd);
        _cmd.Clear();
        
        _camera.TryGetCullingParameters(out ScriptableCullingParameters p);
        _cullingResults = _context.Cull(ref p);

        var sortingSettings = new SortingSettings(_camera);
        var drawingSettings = new DrawingSettings(_unlitShaderTagId, sortingSettings);
        // var filteringSettings = new FilteringSettings(RenderQueueRange.all);
        var filteringSettings = new FilteringSettings(RenderQueueRange.opaque);

        context.DrawRenderers(
            _cullingResults, ref drawingSettings, ref filteringSettings
        );

        context.DrawSkybox(_camera);
        //透明队列没有深度，所以若在skybox前面渲染会导致丢失
        //没有深度是因为透明需要从后往前渲染，才能做透明混合
        filteringSettings.renderQueueRange = RenderQueueRange.transparent;

        context.DrawRenderers(
            _cullingResults, ref drawingSettings, ref filteringSettings
        );
        context.Submit();
    }
}